﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
using System.Xml;


public partial class SJZD_NameManage : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initData();
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            Spec.Text = Request.QueryString["Spec"];
            catalogueName.Text = Request.QueryString["catalogueName"];
            ComodBarcode.Text = Request.QueryString["ComodBarcode"];
            TypeName.Text = Request.QueryString["TypeName"];
            StockNum.SelectedValue = Request.QueryString["StockNum"];
        }
    }

    private void initData()
    {
        ZSql zs = new ZSql();
        string search = "";
        string sql = "select * from(SELECT catalogueId, ComodBarcode, catalogueName, Units, Spec,Prices,ProcMoney, Remark, AuditState, EditUserName,Convert(char(10),EditDate,120) as EditDate ,(SELECT TypeName  FROM SV_Type  WHERE a.TypeId = TypeId) AS TypeName,(SELECT Units FROM Units where a.Units=UnitsId) as UnitsName ,((select isnull(sum(ProcNum),0) from SV_Stock where ComodBarcode=a.ComodBarcode)-(SELECT ISNULL(SUM(OutNum), 0) AS number FROM SV_OutStock WHERE comodBarcode =a.ComodBarcode)) as StockNum FROM SV_Catalogue a) TblCatalogue where 1=1";
        
        if (!string.IsNullOrEmpty(Request.QueryString["catalogueName"]))
        {
            search += "&catalogueName=" + Common.FormatParameter(Request.QueryString["catalogueName"].Trim());
            sql += " and catalogueName like'%" + Request.QueryString["catalogueName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComodBarcode"]))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(Request.QueryString["ComodBarcode"].Trim());
            sql += " and ComodBarcode like'%" + Request.QueryString["ComodBarcode"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["Spec"]))
        {
            search += "&Spec=" + Common.FormatParameter(Request.QueryString["TypeName"].Trim());
            sql += " and Spec like'%" + Request.QueryString["Spec"] + "%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["TypeName"]))
        {
            search += "&TypeName=" + Common.FormatParameter(Request.QueryString["TypeName"].Trim());
            sql += " and TypeName like'%" + Request.QueryString["TypeName"] + "%'";

        }
        if (!string.IsNullOrEmpty(Request.QueryString["StockNum"]))
        {
            search += "&StockNum=" + Request.QueryString["StockNum"];
            sql += "  and StockNum" + Request.QueryString["StockNum"];
        }
        sql += " order by catalogueId desc";
        PageNavigator1.Url = "../Services/CatalogueManage.aspx?moduid=" + Request.QueryString["moduid"].ToString() + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }

    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("CatalogueAdd.aspx?id=" + id + "&moduid=" + hfxs.Value + "&action=update");

    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("CatalogueAdd.aspx?id=" + id + "&moduid=" + hfxs.Value + "&action=flag");

    }

    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();
            if (AuditState == "1")
            {
                e.Row.Cells[11].Text = "已通过";
                e.Row.Cells[11].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                e.Row.Cells[11].Text = "未通过";
                e.Row.Cells[11].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "")
            {
                e.Row.Cells[11].Text = "未审核";
                e.Row.Cells[11].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }

            int isEnd = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "StockNum"));
            //可用状态
            if (isEnd >0)
            {
                (e.Row.FindControl("chkDelete") as CheckBox).Enabled = false;//删除是否可用
            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)//------------------------添加-------------
    {
        Response.Redirect("CatalogueAdd.aspx?moduid=" + hfxs.Value + "&action=add");
    }

    protected void btnallflag_Click(object sender, EventArgs e)//---------------------审核
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    allflag.Open("update SV_Catalogue set AuditState=1 where catalogueId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[11].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    del.Open("delete SV_Catalogue  where catalogueId=" + this.GvData.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功!');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)//生成Excle
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=CatalogueManage" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm. control is rendered for
    }

    //-------------------------xls生成结束----------------------------------

    protected void btnSelec_Click(object sender, EventArgs e)//搜索
    {
        string search = "";
        if (!string.IsNullOrEmpty(Spec.Text.Trim()))
        {
            search += "&Spec=" + Common.FormatParameter(Spec.Text.Trim());
        }
        if (!string.IsNullOrEmpty(catalogueName.Text.Trim()))
        {
            search += "&catalogueName=" + catalogueName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(ComodBarcode.Text))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(TypeName.Text.Trim()))
        {
            search += "&TypeName=" + Common.FormatParameter(TypeName.Text.Trim());
        }
        if (!string.IsNullOrEmpty(StockNum.Text))//库存
        {
            search += "&StockNum=" + Common.FormatParameter(StockNum.Text);
        }
        PageNavigator1.Url = "../Services/CatalogueManage.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("CatalogueManage.aspx?moduid=" + hfxs.Value + search);
    }

}
